package Directed;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;

public class DirectedGraph implements Cloneable {
    private int vertexNum;
    private int edgeNum;
    private TreeSet<Integer>[] adj;
    private boolean directed;

    public DirectedGraph(String fileName, boolean directed) {
        File file = new File(fileName);
        this.directed = directed;
        try (Scanner scanner = new Scanner(file)) {
            vertexNum = scanner.nextInt();
            if (vertexNum < 0) {
                throw new IllegalArgumentException("vertex number must be non-negative");
            }
            adj = new TreeSet[vertexNum];
            for (int i = 0; i < vertexNum; i++) {
                adj[i] = new TreeSet<>();
            }
            edgeNum = scanner.nextInt();
            if (edgeNum < 0) {
                throw new IllegalArgumentException("edge number must be non-negative");
            }
            for (int i = 0; i < edgeNum; i++) {
                int a = scanner.nextInt();
                validateVertex(a);
                int b = scanner.nextInt();
                validateVertex(b);
                if (a == b) {
                    throw new IllegalArgumentException("Self Loop is Detected!");
                }
                if (adj[a].contains(b)) {
                    throw new IllegalArgumentException("Parallel Edges are Detected!");
                }
                adj[a].add(b);
                if (!directed) {
                    adj[b].add(a);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public DirectedGraph(String fileName) {
        this(fileName, false);
    }

    public boolean isDirected() {
        return directed;
    }

    public void validateVertex(int vertexNum) {
        if (vertexNum < 0 || vertexNum >= this.vertexNum) {
            throw new IllegalArgumentException("vertex " + vertexNum + "is invalid.");
        }
    }

    public int getVertexNum() {
        return vertexNum;
    }

    public int getEdgeNum() {
        return edgeNum;
    }

    public boolean hasEdge(int v, int w) {
        validateVertex(v);
        validateVertex(w);
        return adj[v].contains(w);
    }

    public Iterable<Integer> adj(int v) {
        validateVertex(v);
        return adj[v];
    }

//    public int degree(int v) {
//        validateVertex(v);
//        return adj[v].size();
//    }

    public void removeEdge(int v, int w) {
        validateVertex(v);
        validateVertex(w);
        if (adj[v].contains(w)) {
            edgeNum--;
        }
        adj[v].remove(w);
        if (!directed) {
            adj[w].remove(v);
        }
    }

    @Override
    public Object clone() {
        try {
            DirectedGraph cloned = (DirectedGraph) super.clone();
            cloned.adj = new TreeSet[this.vertexNum];
            for (int v = 0; v < this.vertexNum; v++) {
                cloned.adj[v] = new TreeSet<>();
                for (int w : this.adj[v]) {
                    cloned.adj[v].add(w);
                }
            }
            return cloned;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("V = %d, E = %d, directed =%b \n", vertexNum, edgeNum, directed)).append("\n");
        for (int v = 0; v < vertexNum; v++) {
            sb.append(String.format("%d : ", v));
            for (Integer w : adj[v]) {
                sb.append(String.format("%d ", w));
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        DirectedGraph directedGraph = new DirectedGraph("g.txt",true );
        System.out.println(directedGraph);
    }
}
